package com.spider.silence.main;

import com.spider.silence.downloader.ChengDuWanBaoDownloader;
import com.spider.silence.pool.ChengDuWanBaoDownloaderPool;
import com.spider.silence.pool.InformantCenterForSiChuanDownloaderPool;
import com.spider.silence.utils.PropertiesUtil;
import com.spider.silence.worker.ChengDuWanBaoWorker;
import com.spider.silence.worker.InformantCenterForSiChuanWorker;
import org.apache.log4j.PropertyConfigurator;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by admin on 2017/7/4.
 * 成都晚报的爬虫主方法
 */
public class ChengDuWanBaoMain {

    public static void main(String[] args) throws Exception {

        //初始化日志环境
        PropertyConfigurator.configure(PropertiesUtil.getLogPro());
        //初始化下载池
        ChengDuWanBaoDownloaderPool downloaderPool = ChengDuWanBaoDownloaderPool.getInstance();
        new Thread(new Runnable() {
            @Override
            public void run() {
                //获取一个下载器
                ChengDuWanBaoDownloader downloader = null;
                try {
                    downloader = downloaderPool.fetchDownLoader(1000);
                    downloader.generateTask();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    if(downloader != null) {
                        downloaderPool.releaseDownLoader(downloader);
                    }
                }
            }
        }).start();

        ExecutorService pool =  Executors.newFixedThreadPool(3000);
        //初始化中国新闻网四川频道的Worker并且添加到线程池中
        for(int i = 0; i<Integer.valueOf(PropertiesUtil.getVal("ChengDuWanBao.workCount")); i++){
            pool.execute(new ChengDuWanBaoWorker());
        }
        /*ChengDuWanBaoDownloader downloader = new ChengDuWanBaoDownloader();
        downloader.crawlItem("http://www.cdwb.com.cn/html/2017-07/04/content_2374406.htm");
*/


    }


}
